package com.bdqn.vueboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.vueboot.dto.LoginlogDto;
import com.bdqn.vueboot.entity.Loginlog;
import com.bdqn.vueboot.entity.SysUser;
import com.bdqn.vueboot.mapper.SysUserMapper;
import com.bdqn.vueboot.service.LoginlogService;
import com.bdqn.vueboot.mapper.LoginlogMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class LoginlogServiceImpl extends ServiceImpl<LoginlogMapper, Loginlog>
    implements LoginlogService{
    @Resource
    private LoginlogMapper loginlogMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public IPage<LoginlogDto> pages(IPage<Loginlog> login,String loginlogDate,String loginlogDate1) {
        LambdaQueryWrapper<Loginlog> query = new LambdaQueryWrapper<>();
        query.ge(loginlogDate!=null&&loginlogDate!="",Loginlog::getLoginlogDate,loginlogDate);
        query.le(loginlogDate1!=null&&loginlogDate1!="",Loginlog::getLoginlogDate,loginlogDate1);
        query.orderByDesc(Loginlog::getLoginlogDate);
        IPage<Loginlog> loginlogIPage = loginlogMapper.selectPage(login,query);
        IPage<LoginlogDto> dtoIPage=new Page<>();
        List<LoginlogDto> loginlogDtoList = new ArrayList<>();
        BeanUtils.copyProperties(loginlogIPage,dtoIPage);
        for (Loginlog loginlog:loginlogIPage.getRecords()
             ) {
            LoginlogDto loginlogDto=new LoginlogDto();
            BeanUtils.copyProperties(loginlog,loginlogDto);
            loginlogDtoList.add(loginlogDto);

        }
        loginlogDtoList.stream().map((item) -> {

            SysUser sysUser = sysUserMapper.selectById(item.getLoginlogUsrid());
            item.setLoginlogUsrName(sysUser.getUsrName());
            return  item;
        }).collect(Collectors.toList());

        dtoIPage.setRecords(loginlogDtoList);
        return dtoIPage;
    }
}




